<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { StarFilled, Delete } from '@element-plus/icons-vue'

interface Favorite {
  id: string
  title: string
  category: string
  publishTime: string
  favoriteTime: string
}

const favorites = ref<Favorite[]>([
  {
    id: 'FAV001',
    title: '2025年春季高校招生简章发布',
    category: '招生信息',
    publishTime: '2025-01-10',
    favoriteTime: '2025-01-15 14:30'
  },
  {
    id: 'FAV002',
    title: '教师资格证考试报名通知',
    category: '资格考试',
    publishTime: '2025-01-20',
    favoriteTime: '2025-01-22 09:15'
  },
  {
    id: 'FAV003',
    title: '计算机等级考试时间安排',
    category: '等级考试',
    publishTime: '2025-02-01',
    favoriteTime: '2025-02-03 16:45'
  },
  {
    id: 'FAV004',
    title: '研究生入学考试成绩查询公告',
    category: '研究生考试',
    publishTime: '2025-02-10',
    favoriteTime: '2025-02-12 11:20'
  }
])

const handleView = (item: Favorite) => {
  ElMessage.info(`查看收藏：${item.title}`)
}

const handleUnfavorite = (item: Favorite) => {
  ElMessageBox.confirm('确定要取消收藏吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    const index = favorites.value.findIndex(f => f.id === item.id)
    if (index > -1) {
      favorites.value.splice(index, 1)
      ElMessage.success('已取消收藏')
    }
  }).catch(() => {
    // 用户取消操作
  })
}

const getCategoryColor = (category: string): string => {
  const colorMap: Record<string, string> = {
    '招生信息': '#4facfe',
    '资格考试': '#7ee8fa',
    '等级考试': '#ffd700',
    '研究生考试': '#ff6b6b'
  }
  return colorMap[category] || '#4facfe'
}
</script>

<template>
  <div class="favorite-list">
    <div class="list-header">
      <h3 class="list-title">我的收藏</h3>
      <p class="list-desc">您收藏的招考信息列表</p>
    </div>

    <!-- 收藏列表 -->
    <div class="favorite-cards">
      <div v-if="favorites.length === 0" class="empty-state">
        <div class="empty-icon">⭐</div>
        <div class="empty-text">暂无收藏内容</div>
        <div class="empty-hint">快去收藏感兴趣的招考信息吧~</div>
      </div>

      <div
        v-for="item in favorites"
        :key="item.id"
        class="favorite-card"
      >
        <div class="card-header">
          <el-icon :size="20" color="#ffd700">
            <StarFilled />
          </el-icon>
          <span
            class="category-tag"
            :style="{ backgroundColor: getCategoryColor(item.category) }"
          >
            {{ item.category }}
          </span>
        </div>

        <div class="card-body">
          <h4 class="favorite-title" @click="handleView(item)">{{ item.title }}</h4>
          <div class="favorite-meta">
            <span class="meta-item">
              <span class="meta-label">发布时间：</span>
              <span class="meta-value">{{ item.publishTime }}</span>
            </span>
            <span class="meta-divider">|</span>
            <span class="meta-item">
              <span class="meta-label">收藏时间：</span>
              <span class="meta-value">{{ item.favoriteTime }}</span>
            </span>
          </div>
        </div>

        <div class="card-footer">
          <el-button type="primary" plain size="default" @click="handleView(item)">
            查看详情
          </el-button>
          <el-button
            :icon="Delete"
            type="danger"
            plain
            size="default"
            @click="handleUnfavorite(item)"
          >
            取消收藏
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.favorite-list {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 32px;
  box-shadow: 0 4px 16px rgba(79, 172, 254, 0.1);
}

.list-header {
  margin-bottom: 24px;
  padding-bottom: 20px;
  border-bottom: 2px solid #f0f9ff;
}

.list-title {
  font-size: 22px;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0 0 8px 0;
}

.list-desc {
  font-size: 14px;
  color: var(--text-secondary);
  margin: 0;
}

/* 收藏卡片 */
.favorite-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 20px;
}

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 60px 20px;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 16px;
}

.empty-text {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.empty-hint {
  font-size: 14px;
  color: var(--text-secondary);
}

.favorite-card {
  background: #ffffff;
  border: 2px solid #f0f0f0;
  border-radius: 12px;
  padding: 20px;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.favorite-card:hover {
  border-color: var(--primary-color);
  box-shadow: 0 8px 24px rgba(79, 172, 254, 0.15);
  transform: translateY(-4px);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 12px;
}

.category-tag {
  padding: 4px 12px;
  border-radius: 12px;
  color: #ffffff;
  font-size: 13px;
  font-weight: 500;
}

.card-body {
  flex: 1;
}

.favorite-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 12px 0;
  cursor: pointer;
  line-height: 1.5;
  transition: color 0.3s ease;
}

.favorite-title:hover {
  color: var(--primary-color);
}

.favorite-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  flex-wrap: wrap;
}

.meta-item {
  display: inline-flex;
  align-items: center;
}

.meta-label {
  color: var(--text-secondary);
}

.meta-value {
  color: var(--text-primary);
  font-weight: 500;
}

.meta-divider {
  color: #e0e0e0;
}

.card-footer {
  display: flex;
  gap: 12px;
  padding-top: 16px;
  border-top: 1px solid #f0f0f0;
}

.card-footer .el-button {
  flex: 1;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .favorite-list {
    padding: 20px;
  }

  .favorite-cards {
    grid-template-columns: 1fr;
  }

  .card-footer {
    flex-direction: column;
  }
}
</style>
